﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using EIIS.Infrastructure.Model.System.Authority;
using EIIS.Infrastructure.Repositories.System.Authority;
using EIIS.Infrastructure.Repositories;

namespace EIIS.Infrastructure.Repositories.Implement
{
    public class HospUserRepository:RepositoryBase<HospUser>,IHospUserRepository
    {
        public bool UpdatePasswordByName(string strLoginName, string strNewPassword)
        {

            string strQueryString = "update HospUser set Pwd=:Pwd where LoginName=:LoginName";
            return Session.CreateQuery(strQueryString)
                .SetString("Pwd", strNewPassword)
                .SetString("LoginName", strLoginName)
                .ExecuteUpdate() > 0;
        }

        public bool UpdatePasswordByID(string strId, string strNewPassword)
        {
            string strQueryString = "update HospUser set Pwd=:Pwd where Id=:Id";
            return Session.CreateQuery(strQueryString)
                .SetString("Pwd", strNewPassword)
                .SetString("Id", strId)
                .ExecuteUpdate() > 0;
        }

        public HospUser GetUserByName(string strLoginName)
        {
            HospUser user = null;
            string strQueryString = "select u from HospUser u where u.LoginName=:LoginName";
            var query = Session.CreateQuery(strQueryString)
                .SetString("LoginName", strLoginName)
                .List<HospUser>();
            if (query != null && query.LongCount() > 0) user = query.First();
            return user;
        }

        public bool ValidateByName(string strLoginName, string strPassword)
        {
            var query = Session.CreateQuery(@"select count(u) from HospUser as u 
                where u.LoginName=:LoginName and u.Pwd=:Pwd and u.State=:State")
                .SetString("LoginName", strLoginName)
                .SetString("Pwd", strPassword)
                .SetEnum("State", PersonState.Normal)
                .UniqueResult<long>();
            return query == 1;
        }

        public bool ValidateByID(string strId, string strPassword)
        {
            var query = Session.CreateQuery(@"select count(u) from HospUser as u 
                where u.Id=:Id and u.Pwd=:Pwd and u.State=:State")
                .SetString("Id", strId)
                .SetString("Pwd", strPassword)
                .SetEnum("State", PersonState.Normal)
                .UniqueResult<long>();
            return query == 1;
        }

        #region IHospUserRepository Members


        public IQueryable<HospUser> LoadAllWithPage(out long total, int page, int rows, string order, string sort)
        {
            var list = this.LoadAll();
            total = list.LongCount();
            if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(sort))
            {
                list = list.OrderBy(sort + " " + order);
            }
            list = list.Skip((page - 1) * rows).Take(rows);
            return list;
        }

        #endregion
    }
}
